package com.example.demo.service.impl;

import com.example.demo.entity.Safetyinspectionanalogsensor;
import com.example.demo.entity.Safetyinspectionspatialdata;
import com.example.demo.handle.AnalogSensorListParseHandler;
import com.example.demo.handle.SafetyMonitoringListParseHandler;
import com.example.demo.mapper.SafetyinspectionanalogsensorMapper;
import com.example.demo.service.SafetyinspectionanalogsensorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-15
 */
@Service
public class SafetyinspectionanalogsensorServiceImpl extends ServiceImpl<SafetyinspectionanalogsensorMapper, Safetyinspectionanalogsensor> implements SafetyinspectionanalogsensorService {

    @Autowired
    private SafetyinspectionanalogsensorMapper safetyinspectionanalogsensorMapper;

    @Override
    public Integer insertAnalogSensorData(String analogSensorXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            AnalogSensorListParseHandler analogSensorListParseHandler = new AnalogSensorListParseHandler();
            try {
                //读取井口信息文件
                parser.parse(analogSensorXml, analogSensorListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + analogSensorListParseHandler.getSafetyinspectionanalogsensorArrayList().size() + "个信息");
            // 根据minecode去查找数据库
            for (Safetyinspectionanalogsensor safetyinspectionanalogsensor : analogSensorListParseHandler.getSafetyinspectionanalogsensorArrayList()) {
                System.out.println(safetyinspectionanalogsensor.getCsMineCode());
                System.out.println(safetyinspectionanalogsensor.getCsDataTime());
                System.out.println(safetyinspectionanalogsensor.getSsStationCode());
                System.out.println(safetyinspectionanalogsensor.getSsTransducerCode());
                System.out.println(safetyinspectionanalogsensor.getSsTransducerName());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogUnit());
                System.out.println(safetyinspectionanalogsensor.getSsTransducerPoint());
                System.out.println(safetyinspectionanalogsensor.getSsGisnodeCode());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogLower());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogHigh());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogAlarmlow());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogAlarmhigh());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogPofflow());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogPoffhigh());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogPonlow());
                System.out.println(safetyinspectionanalogsensor.getSsAnalogPonhigh());
                System.out.println(safetyinspectionanalogsensor.getSsPoffareaCode());

                System.out.println("----------------------------安全监控信息finish---------------------------------");

                safetyinspectionanalogsensorMapper.insertAnalogSensorData(safetyinspectionanalogsensor.getCsMineCode(),safetyinspectionanalogsensor.getCsDataTime(),safetyinspectionanalogsensor.getSsStationCode(),safetyinspectionanalogsensor.getSsTransducerCode(),safetyinspectionanalogsensor.getSsTransducerName(),safetyinspectionanalogsensor.getSsAnalogUnit(),safetyinspectionanalogsensor.getSsTransducerPoint(),safetyinspectionanalogsensor.getSsGisnodeCode(),safetyinspectionanalogsensor.getSsAnalogLower(),safetyinspectionanalogsensor.getSsAnalogHigh(),safetyinspectionanalogsensor.getSsAnalogAlarmlow(),safetyinspectionanalogsensor.getSsAnalogAlarmhigh(),safetyinspectionanalogsensor.getSsAnalogPofflow(),safetyinspectionanalogsensor.getSsAnalogPoffhigh(),safetyinspectionanalogsensor.getSsAnalogPonlow(),safetyinspectionanalogsensor.getSsAnalogPonhigh(),safetyinspectionanalogsensor.getSsPoffareaCode());
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
